package com.google.appengine.api.datastore;

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import lombok.Getter;

/**
 *
 * @author Secondary User
 */
public class Query
{
	@Getter
	private String kind;
	@Getter
	private Key ancestor;
	@Getter
	private boolean keysOnly;
	private boolean distinct;

	public Query(String kind, Key ancestor)
	{
		this.kind = kind;
		this.ancestor = ancestor;
	}

	public Query(String kind)
	{
		this.kind = kind;
		this.ancestor = null;
	}

	public Query()
	{
		this.kind = null;
		this.ancestor = null;
	}

	public Query setKeysOnly()
	{
		this.keysOnly = true;
		return this;
	}

	public Query addFilter(String prop, FilterOperator op, Object value)
	{
		throw new UnsupportedOperationException("Not yet implemented");
		//return this;
	}

	public Query addSort(String condition, SortDirection dir)
	{
		throw new UnsupportedOperationException("Not yet implemented");
		//return this;
	}

	public Query setAncestor(Key ancestor)
	{
		this.ancestor = ancestor;
		return this;
	}

	public List<FilterPredicate> getFilterPredicates()
	{
		return new ArrayList<FilterPredicate>();
	}

	public List<SortPredicate> getSortPredicates()
	{
		return new ArrayList<SortPredicate>();
	}

	public Query setDistinct(boolean value)
	{
		distinct = value;
		return this;
	}

	public boolean getDistinct()
	{
		return distinct;
	}

	public boolean needsIndex()
	{
		//TODO: Change this when we implement queries that need index tables.
		return (ancestor != null && ancestor.isComplete()) || (kind != null && !kind.isEmpty());
	}

	public static enum FilterOperator
	{

		EQUAL, NOT_EQUAL, IN, GREATER_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL;
	}

	@Data
	public static class FilterPredicate
	{

		private final String propertyName;
		private final Object value;
		private final FilterOperator operator;
	}

	public static enum SortDirection
	{
		ASCENDING, DESCENDING;
	}

	@Data
	public static class SortPredicate
	{

		private final String propertyName;
		private final SortDirection direction;
	}
}
